﻿namespace AC.ExtendedRenderer.Toolkit
{
    using AC.ExtendedRenderer.Toolkit.Drawing;
    using ComponentFactory.Krypton.Toolkit;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;

    [ToolboxBitmap(typeof(Panel))]
    public class KryptonPanel : Panel
    {
        private Color _borderColor = Color.Gray;
        private int _borderWidth;
        private LinearGradientMode _gradientDirection = LinearGradientMode.Vertical;
        private Color _gradientEndColor = Color.Gray;
        private Color _gradientSolidColor = Color.Gray;
        private Color _gradientStartColor = Color.White;
        private bool _gradientToogleColors;
        private bool _gradientUseBlend;
        private bool _gradientUseSolid = true;
        private static IPalette _palette;
        private static PaletteRedirect _paletteRedirect;
        private bool _persistentColors;

        public KryptonPanel()
        {
            base.SetStyle(ControlStyles.DoubleBuffer, true);
            base.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            base.SetStyle(ControlStyles.ResizeRedraw, true);
            base.SetStyle(ControlStyles.UserPaint, true);
            base.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            base.UpdateStyles();
            if (_palette != null)
            {
                _palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
            }
            KryptonManager.GlobalPaletteChanged += new System.EventHandler(this.OnGlobalPaletteChanged);
            _palette = KryptonManager.CurrentGlobalPalette;
            _paletteRedirect = new PaletteRedirect(_palette);
            this.InitColors();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_palette != null)
                {
                    _palette.PalettePaint -= new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
                    _palette = null;
                }
                KryptonManager.GlobalPaletteChanged -= new System.EventHandler(this.OnGlobalPaletteChanged);
            }
            base.Dispose(disposing);
        }

        private void InitColors()
        {
            this._gradientSolidColor = _palette.ColorTable.MenuStripGradientEnd;
            this._gradientStartColor = _palette.ColorTable.StatusStripGradientBegin;
            this._gradientEndColor = _palette.ColorTable.StatusStripGradientEnd;
            this._borderColor = _palette.ColorTable.ToolStripGradientEnd;
        }

        private void OnGlobalPaletteChanged(object sender, EventArgs e)
        {
            if (_palette != null)
            {
                _palette.PalettePaint -= new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
            }
            _palette = KryptonManager.CurrentGlobalPalette;
            _paletteRedirect.Target = _palette;
            if (_palette != null)
            {
                _palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
                if (!this._persistentColors)
                {
                    this.InitColors();
                }
            }
            base.Invalidate();
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            Color color;
            Color color2;
            base.OnPaintBackground(e);
            Rectangle rect = new Rectangle(0, 0, base.Width, base.Height);
            Rectangle rectangle2 = new Rectangle(0, 0, base.Width - 1, base.Height - 1);
            if (!this._gradientToogleColors)
            {
                color = this._gradientStartColor;
                color2 = this._gradientEndColor;
            }
            else
            {
                color = this._gradientEndColor;
                color2 = this._gradientStartColor;
            }
            if (!this._gradientUseSolid)
            {
                if (!this._gradientUseBlend)
                {
                    using (LinearGradientBrush brush = new LinearGradientBrush(rect, color, color2, this.GradientDirection))
                    {
                        e.Graphics.FillRectangle(brush, rect);
                        goto Label_00E0;
                    }
                }
                DrawingMethods.DrawGradient(e.Graphics, rect, color, color2, 90f, false, Color.White, 0f);
            }
            else
            {
                using (Brush brush2 = new SolidBrush(this.GradientSolidColor))
                {
                    e.Graphics.FillRectangle(brush2, rect);
                }
            }
        Label_00E0:
            if (this._borderWidth > 0)
            {
                e.Graphics.DrawRectangle(new Pen(Color.FromArgb(180, this._borderColor), (float) this._borderWidth), rectangle2);
            }
        }

        private void OnPalettePaint(object sender, PaletteLayoutEventArgs e)
        {
            base.Invalidate();
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Category("Appearance-Extended"), Browsable(true)]
        public Color BorderColor
        {
            get
            {
                return this._borderColor;
            }
            set
            {
                this._borderColor = value;
                base.Invalidate();
            }
        }

        [Category("Appearance-Extended"), Browsable(true)]
        public int BorderWidth
        {
            get
            {
                return this._borderWidth;
            }
            set
            {
                this._borderWidth = value;
                base.Invalidate();
            }
        }

        [Category("Appearance-Extended"), Browsable(true)]
        public LinearGradientMode GradientDirection
        {
            get
            {
                return this._gradientDirection;
            }
            set
            {
                this._gradientDirection = value;
                base.Invalidate();
            }
        }

        [Category("Appearance-Extended"), Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Color GradientEndColor
        {
            get
            {
                return this._gradientEndColor;
            }
            set
            {
                this._gradientEndColor = value;
                base.Invalidate();
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(true), Category("Appearance-Extended")]
        public Color GradientSolidColor
        {
            get
            {
                return this._gradientSolidColor;
            }
            set
            {
                this._gradientSolidColor = value;
                base.Invalidate();
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(true), Category("Appearance-Extended")]
        public Color GradientStartColor
        {
            get
            {
                return this._gradientStartColor;
            }
            set
            {
                this._gradientStartColor = value;
                base.Invalidate();
            }
        }

        [Browsable(true), Category("Appearance-Extended")]
        public bool GradientToogleColors
        {
            get
            {
                return this._gradientToogleColors;
            }
            set
            {
                this._gradientToogleColors = value;
                base.Invalidate();
            }
        }

        [Category("Appearance-Extended"), Browsable(true)]
        public bool GradientUseBlend
        {
            get
            {
                return this._gradientUseBlend;
            }
            set
            {
                this._gradientUseBlend = value;
                base.Invalidate();
            }
        }

        [Browsable(true), Category("Appearance-Extended")]
        public bool GradientUseSolid
        {
            get
            {
                return this._gradientUseSolid;
            }
            set
            {
                this._gradientUseSolid = value;
                base.Invalidate();
            }
        }

        [Browsable(true), Category("Appearance-Extended")]
        public bool PersistentColors
        {
            get
            {
                return this._persistentColors;
            }
            set
            {
                this._persistentColors = value;
                base.Invalidate();
            }
        }
    }
}

